package com.ybServer.service.impl;

import com.ybServer.dao.ActivityDAO;
import com.ybServer.dao.CommentDAO;
import com.ybServer.dao.JoinDAO;
import com.ybServer.model.ActivityData;
import com.ybServer.model.Comment;
import com.ybServer.model.User;
import com.ybServer.service.ActivityService;
import com.ybServer.util.Constant;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;


@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityDAO activityDAO;
    @Resource
    private CommentDAO commentDAO;
    @Resource
    private JoinDAO joinDAO;

    @Override
    public void saveActivity(MultipartFile[] pictures, ActivityData activityData) throws Exception {

        activityDAO.addActivity(activityData);
        //插入数据库后，得到自增id
        Long activityId = activityData.getId();
        if (pictures != null) {
            for (MultipartFile picture : pictures) {
                /**上传得到的文件名 */
                String fileName = picture.getOriginalFilename();
                /**文件名后缀*/
                //String suffix = fileName.substring(fileName.lastIndexOf("."));
                /**存入数据库的文件名*/
                String storageName = activityId + "." + fileName;
                /**创建文件的虚拟路径*/
                String virtualPath = Constant.STORAGE_PATH + "activityPictures/" + storageName;
                File saveFile = new File(virtualPath);
                activityDAO.addActivityImage(activityId + "", storageName);
                if (saveFile.exists())
                    saveFile.delete();
                picture.transferTo(saveFile);
            }
        }

    }

    @Override
    public List<ActivityData> queryActivityByCategory(String category, long lastId) throws Exception {

        List<ActivityData> activityDatas;
        activityDatas = activityDAO.selectActivityByCategory(category, lastId);


        if (activityDatas != null && activityDatas.size() != 0) {
            for (ActivityData activityData : activityDatas) {
                Long activityId = activityData.getId();
                activityData.setImageNames(activityDAO.selectActivityImage(activityId));
                activityData.setComments(commentDAO.selectComments(activityId));
                activityData.setJoinNum(joinDAO.queryJoinUserNumByActivityId(activityId));
            }
        }
        return activityDatas;
    }

    @Override
    public List<ActivityData> queryActivityByUserId(String userId, long lastId) throws Exception {
        List<ActivityData> activityDatas;
        activityDatas = activityDAO.selectActivityByUserId(userId, lastId);
        if (activityDatas != null && activityDatas.size() != 0) {
            for (ActivityData activityData : activityDatas) {
                long activityId = activityData.getId();
                activityData.setImageNames(activityDAO.selectActivityImage(activityId));
                activityData.setComments(commentDAO.selectComments(activityId));
                activityData.setJoinNum(joinDAO.queryJoinUserNumByActivityId(activityId));
            }
        }
        return activityDatas;
    }


    @Override
    public byte[] getPictureBytesByFileName(String fileName) throws Exception {
        File file = new File(Constant.STORAGE_PATH + Constant.ACTIVITY_PICTURES_PATH + fileName);
        FileInputStream inputStream = new FileInputStream(file);
        int i = inputStream.available();
        byte[] buff = new byte[i];
        inputStream.read(buff);
        inputStream.close();
        return buff;
    }

    @Override
    public void saveActivityComment(Comment comment) throws Exception {
        commentDAO.addComment(comment);
    }


    @Override
    public void addJoinUser(long activityId, long userId) throws Exception {
        joinDAO.addJoinUser(activityId, userId);
    }

    @Override
    public boolean hasJoin(long activity, long userId) throws Exception {
        return joinDAO.hasJoin(activity, userId) > 0;
    }

    @Override
    public void exitActivity(long activityId, long userId) throws Exception {
        joinDAO.exitActivity(activityId, userId);
    }

    @Override
    public List<User> queryJoinList(long activityId) throws Exception {
        return joinDAO.queryJoinUserByActivityId(activityId);
    }

    @Override
    public int queryJoinNum(long activityId) throws Exception {
        return joinDAO.queryJoinUserNumByActivityId(activityId);
    }

    @Override
    public List<ActivityData> queryHotActivity() throws Exception {

        List<ActivityData> activityDatas;
        activityDatas = activityDAO.queryHotActivity();
        if (activityDatas != null && activityDatas.size() != 0) {
            for (ActivityData activityData : activityDatas) {
                Long activityId = activityData.getId();
                activityData.setImageNames(activityDAO.selectActivityImage(activityId));
                activityData.setComments(commentDAO.selectComments(activityId));
                activityData.setJoinNum(joinDAO.queryJoinUserNumByActivityId(activityId));
            }
        }

        return activityDatas;
    }
}
